/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.surveilans.model;

import com.surveilans.classes.DataSurveilans;
import com.surveilans.classes.InfoSurveilansHari;
import com.surveilans.classes.SMS;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Master
 */
public class SMSModel extends Model {

    private DataModel data;
    private UnitModel unit;
    private InfoModel info;
    
    public SMSModel() {
        super();
        data = new DataModel();
        unit = new UnitModel();
        info = new InfoModel();
    }
    
    public ArrayList<SMS> getNewSMS(){
        ArrayList<SMS> ap = new ArrayList<SMS>();
        
        Statement stmt = null;
        try {
            stmt = con.createStatement();
            String query="SELECT * FROM inbox  WHERE Processed='false'";
            ResultSet rs = stmt.executeQuery(query);

            while (rs.next()) {
                SMS s = new SMS();
                s.setId(rs.getInt("ID"));
                s.setFrom(rs.getString("SenderNumber"));
                s.setText(rs.getString("TextDecoded"));
                ap.add(s);
            }
        } catch (SQLException ex) {
            Logger.getLogger(SMSModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ap;
    }
    
    public void setSMSProcessed(int id_sms){
        PreparedStatement stmt = null;
        try {
            String query = "UPDATE inbox "
                    + "SET Processed='true' "
                    + "WHERE ID="+id_sms;
            stmt = con.prepareStatement(query,Statement.RETURN_GENERATED_KEYS);
            stmt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(SMSModel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void sendSMS(String to, String text) {
        PreparedStatement stmt = null;
        try {
            String query = "INSERT INTO outbox "
                    + "(DestinationNumber,Coding,TextDecoded,CreatorID) "
                    + "VALUES ('"+to+"','Default_No_Compression','"+text+"','admin')";
            stmt = con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
            stmt.getGeneratedKeys();
            stmt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(SMSModel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void smsError(String to) {
        this.sendSMS(to, "Format SMS yang ada kirimkan salah,ketik HELP untuk bantuan");
    }

    public void processData(String from, String text) {
        String[] args = text.toLowerCase().split(" ");
        String username = args[1];
        String password = args[2];
        int id_peny = data.getIdPenyakit(args[3]);
        int r1 = Integer.parseInt(args[4]);
        int r2 = Integer.parseInt(args[5]);
        int r3 = Integer.parseInt(args[6]);
        int r4 = Integer.parseInt(args[7]);
        int r5 = Integer.parseInt(args[8]);
        int r6 = Integer.parseInt(args[9]);
        int r7 = Integer.parseInt(args[10]);
        int r8 = Integer.parseInt(args[11]);
        int r9 = Integer.parseInt(args[12]);
        int r10 = Integer.parseInt(args[13]);
        int r11 = Integer.parseInt(args[14]);
        int r12 = Integer.parseInt(args[15]);
        int unit_id = unit.validate(username, password);
        if(unit_id == 0){
            //username password tidak valid
            this.sendSMS(from, "kode unit dan password anda tidak valid");
        }else if(id_peny == 0){
            //kode penyakit salah
            this.sendSMS(from, "kode penyakit tidak valid");
        }else{
            //valid
            DataSurveilans dp = new DataSurveilans();
            dp.setId_penyakit(id_peny);
            dp.setId_unit(unit_id);
            dp.setRange1(r1);
            dp.setRange2(r2);
            dp.setRange3(r3);
            dp.setRange4(r4);
            dp.setRange5(r5);
            dp.setRange6(r6);
            dp.setRange7(r7);
            dp.setRange8(r8);
            dp.setRange9(r9);
            dp.setRange10(r10);
            dp.setRange11(r11);
            dp.setRange12(r12);
            data.addData(dp);
            this.sendSMS(from, "data surveilans telah berhasil simasukkan ke dalam sistem");
        }
    }
    
    public void processInfo(String from,String text){
        String[] args = text.toLowerCase().split(" ");
        String kode_penyakit = args[1];
        String kode_kabupaten = args[2];
        String tanggal = args[3];
        DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd");
        int id_peny = data.getIdPenyakit(kode_penyakit);
        int id_kab = data.getIdKabupaten(kode_kabupaten);
        String kab_str = info.getNamaKabupaten(id_kab);
        Date date = null;
        Calendar cal = Calendar.getInstance();
        try {
            date = dfm.parse(tanggal);
            cal.setTime(date);
        } catch (ParseException ex) {
            Logger.getLogger(SMSModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        if(date==null){
            this.sendSMS(from, "Format tanggal yang anda kirimkan salah");
        }else if(id_kab==0){
            this.sendSMS(from, "Kode kabupaten yang anda kirimkan salah");
        }else if(id_peny==0){
            this.sendSMS(from, "Kode penyakit yang anda kirimkan salah");
        }else{
            InfoSurveilansHari is = info.getInfo(id_peny, id_kab, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DATE));
            String smsText = "Info Surveilans\n"
                    + kab_str+" "+tanggal+"\n"
                    + "0-7 hr: "+is.getRange(0)+"\n"
                    + "8-28 hr: "+is.getRange(1)+"\n"
                    + "<1 : "+is.getRange(2)+"\n"
                    + "1-4 : "+is.getRange(3)+"\n"
                    + "5-9 : "+is.getRange(4)+"\n"
                    + "10-14 : "+is.getRange(5)+"\n"
                    + "15-19 : "+is.getRange(6)+"\n"
                    + "20-44 : "+is.getRange(7)+"\n"
                    + "45-54 : "+is.getRange(8)+"\n"
                    + "55-59 : "+is.getRange(9)+"\n"
                    + "60-69 : "+is.getRange(10)+"\n"
                    + "70+ : "+is.getRange(11);
            this.sendSMS(from, smsText);
        }
    }
}
